<?php

namespace App\Listeners;

use App\Common\Constant\Params;
use App\Events\MasterOrderRefund;
use App\Events\MasterReturnFund;
use App\Events\OrderRefunded;
use App\Models\MasterFund;
use App\Models\OrderRefund;
use App\Models\OrderStatus;
use App\Notifications\OrderNotifications\OrderRefundNotification;
use App\Services\ISystemService;
use App\Services\ISystemTaskService;
use DB;
use Illuminate\Cache\Events\CacheHit;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;

class MasterReturnFundListener implements ShouldQueue
{

    use InteractsWithQueue;

    public $queue = 'finance';

    public $tries = 3;

    public $timeout = 30;

    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  CacheHit  $event
     * @return void
     */
    public function handle(MasterReturnFund $event)
    {
        $fund = MasterFund::find($event->fund_id);
        if(!$fund){
            throw new \Exception('miss fund');
        }

        $ret = resolve(ISystemTaskService::class)->AssureFundReturn($fund->id);
        if(!$ret){
            throw new \Exception('assure fund return fail');
        }
    }

    public function failed(MasterReturnFund $event, \Exception $exception)
    {
        //
    }
}
